﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls.DataVisualization.Charting;
using ViCode_LeVi.Controls.Charts;
using dotnetCHARTING.WinForms;
using ViCode_LeVi.Controls.Reports;
using DevExpress.XtraReports.UI;
using System.Collections.Specialized;
namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabAnalysis_Tab2_Win.xaml
    /// </summary>
    public partial class VMTabAnalysis_Tab2_Win : UserControl
    {
        public VMTabAnalysis_Tab2_Win()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabAnalysis_Tab2_Win_Loaded);
            RefreshChart();
            
        }

        void VMTabAnalysis_Tab2_Win_Loaded(object sender, RoutedEventArgs e)
        {
            BarChartQQS.Func_GetStep_TaskItemGroup = () =>
            {
                var codes = List_CheckBox.Where(c => c.IsChecked == true).Select(c => c.DataContext as Step_TaskItemGroup).Where(c => c != null).Select(c => c.Code);
                return RootData.Intance.Steps[3].TaskItemStep.TaskItemGroups_Source.Where(c => codes.Contains(c.Code)).ToArray();
            };
            Option_RunFirst();
            RefreshChart();
        }
        #region Option
        private void Option_RunFirst()
        {
            StackPanel_TaskType_LoadData();
            ComboBox_ChartLabelMode_LoadData();
            ComboBox_ChartLegendMode_Setup();
        }
        private void ComboBox_ChartLegendMode_Setup()
        {
            ComboBox_ChartLegendMode.ItemsSource = Enum.GetValues(typeof(eChartLegendMode));
            ComboBox_ChartLegendMode.SelectedIndex = 0;

            BarChartQQS.ChartLegendMode = (eChartLegendMode)ComboBox_ChartLegendMode.SelectedValue;
            ComboBox_ChartLegendMode.SelectionChanged -= new SelectionChangedEventHandler(ComboBox_ChartLegendMode_SelectionChanged);
            ComboBox_ChartLegendMode.SelectionChanged += new SelectionChangedEventHandler(ComboBox_ChartLegendMode_SelectionChanged);

            BarChartQQS.LegendWithPercent = CheckBox_LegendWithPercent.IsChecked == true;
            CheckBox_LegendWithPercent.Click -= new RoutedEventHandler(CheckBox_LegendWithPercent_Click);
            CheckBox_LegendWithPercent.Click += new RoutedEventHandler(CheckBox_LegendWithPercent_Click);
        }

        void CheckBox_LegendWithPercent_Click(object sender, RoutedEventArgs e)
        {
            BarChartQQS.LegendWithPercent = CheckBox_LegendWithPercent.IsChecked == true;
            RefreshChart();
        }

        void ComboBox_ChartLegendMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_ChartLegendMode.SelectedValue != null)
            {
                BarChartQQS.ChartLegendMode = (eChartLegendMode)ComboBox_ChartLegendMode.SelectedValue;
                RefreshChart();
            }
        }
       

        private void ComboBox_ChartLabelMode_LoadData()
        {
            ComboBox_ChartLabelMode.ItemsSource = Enum.GetValues(typeof(eChartLabelMode));
            ComboBox_ChartLabelMode.SelectedIndex = 0;

            BarChartQQS.ChartLabelMode = (eChartLabelMode)ComboBox_ChartLabelMode.SelectedValue;
            ComboBox_ChartLabelMode.SelectionChanged -= new SelectionChangedEventHandler(ComboBox_ChartLabelMode_SelectionChanged);
            ComboBox_ChartLabelMode.SelectionChanged += new SelectionChangedEventHandler(ComboBox_ChartLabelMode_SelectionChanged);

        }

        void ComboBox_ChartLabelMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_ChartLabelMode.SelectedValue != null)
            {
                BarChartQQS.ChartLabelMode = (eChartLabelMode)ComboBox_ChartLabelMode.SelectedValue;
                RefreshChart();
            }
        }

        List<RadioButton> List_CheckBox = new List<RadioButton>();
        private void StackPanel_TaskType_LoadData()
        {
            StackPanel_TaskType.Children.Clear();
            List_CheckBox.Clear();
            var data = RootData.Intance.TaskItemGroupOfStep4.ToArray();
            foreach (var d in data)
            {
                RadioButton chk = new RadioButton() { GroupName="VMTabAnalysis_Tab2_Win", DataContext = d, Content = d.Code, IsChecked = false };
                StackPanel_TaskType.Children.Add(chk);
                List_CheckBox.Add(chk);
                chk.Click -= new RoutedEventHandler(chk_Click);
                chk.Click += new RoutedEventHandler(chk_Click);
            }
            List_CheckBox[0].IsChecked = true;
            RefreshChart();
        }

        void chk_Click(object sender, RoutedEventArgs e)
        {
            RefreshChart();
        }
        #endregion

        public void RefreshChart()
        {
            BarChartQQS.CreateChart();           
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ChartReport rpt = new ChartReport();
            rpt.Title = "Valuation Bar Chart";
            rpt.AddChart(BarChartQQS);
            rpt.CreateReport(new StringCollection());
            ReportTemplate.ShowReportPreview(rpt);
        }
    }
}
